package sparrowGui.skin
{
	import flash.display.DisplayObject;
	import flash.display.Shape;
	import flash.events.Event;
	
	import sparrowGui.Component;
	import sparrowGui.ComponentDraw;
	import sparrowGui.SparrowGUI;
	import sparrowGui.VScrollBar;
	import sparrowGui.item.Item;
	import sparrowGui.utils.PubFun;
	
	public class ScrollBarSkin extends Component
	{
		public var upBtn:Item;
		public var downBtn:Item;
		public var slider:Item;
		public var skinbg:DisplayObject;
		
		public function ScrollBarSkin()
		{
//			var vars:ComponentVars = SparrowGUI.getIns().scrollBarVar;
			
			super();
			
			upBtn.onInvalidate(null);
			downBtn.onInvalidate(null);
			slider.onInvalidate(null);
		}
		
		override protected function init():void
		{
/*			var vars:ComponentVars = new ComponentVars();
			vars.alpha = 0.2;
			vars.bgColor = 0xFFFFFF;*/
			skinbg = new ComponentDraw(SparrowGUI.getIns().scrollBarVar);
			skinbg.width = VScrollBar.THICKNESS;
			skinbg.name = "skinbg";
			
//			var btnSty:BaseComponentVars = new BaseComponentVars();
//			btnSty.width = btnSty.height = VScrollBar.THICKNESS;
//			btnSty.isBgFill = true;
			
			upBtn = new Item();
			downBtn = new Item();
			
//			var bs:Object = PubFun.copyObj(btnSty);
//			bs.isIntXY = false;
			slider = new Item();
			slider.isIntXY = false;
			slider.isBgFill = true;
			slider.width = VScrollBar.THICKNESS;
			slider.height = VScrollBar.THICKNESS;
			
			var arrSp:Shape = PubFun.drawArrow(1);
			arrSp.x = arrSp.y = 2;
			upBtn.addChild(arrSp);
			
			arrSp = PubFun.drawArrow(0);
			arrSp.x = arrSp.y = 2;
			downBtn.addChild(arrSp);
			
			upBtn.name = "upBtn";
			slider.name = "slider";
			downBtn.name = "downBtn";
			
			addChild(skinbg);
			addChild(slider);
			addChild(upBtn);
			addChild(downBtn);
			
			refush();
		}
		
		private function refush():void
		{
			upBtn.setSize(VScrollBar.THICKNESS,VScrollBar.THICKNESS);
			upBtn.y = 0;
			
			downBtn.setSize(VScrollBar.THICKNESS,VScrollBar.THICKNESS);
			downBtn.y = style.height - VScrollBar.THICKNESS;
			
			var minH:int = VScrollBar.THICKNESS*2 + VScrollBar.MIN_SLIDER_HEIGHT;
			if(style.height<minH){
				slider.visible = false;
				return;
			}
			slider.setSize(VScrollBar.THICKNESS,(style.height-VScrollBar.THICKNESS*2));
			slider.y = VScrollBar.THICKNESS;
		}
		
		override protected function draw():void
		{
			if(upBtn){
				upBtn.x = style.paddingLeft;
				upBtn.y = style.paddingTop;
				upBtn.setSize(VScrollBar.THICKNESS,VScrollBar.THICKNESS);
			}
			if(downBtn){
				downBtn.x = style.paddingLeft;
				downBtn.y = style.height - downBtn.height - style.paddingBottom;
				downBtn.setSize(VScrollBar.THICKNESS,VScrollBar.THICKNESS);
			}
			if(slider){
				slider.x = style.paddingLeft;
				slider.y = upBtn.y + upBtn.height;
//				slider.setSize(ScrollBar.THICKNESS,ScrollBar.MIN_SLIDER_HEIGHT);
				var minH:int = VScrollBar.THICKNESS*2 + VScrollBar.MIN_SLIDER_HEIGHT;
				if(style.height<minH){
					slider.visible = false;
					return;
				}
//				slider.setSize(ScrollBar.THICKNESS,
//					(style.height-ScrollBar.THICKNESS*2-style.paddingTop-style.paddingBottom));
			}
			super.draw();
		}
		
		override public function set height(h:Number):void
		{
			style.height = h;
			skinbg.height = h;
			draw();
			dispatchEvent(new Event(Event.RESIZE));
		}
	}
}